<template>
  <div class="user-list-container">
    <el-card>
      <el-table
        :data="list"
        v-loading="loading"
        style="width: 100%"
        empty-text="暂无数据"
      >
        <el-table-column prop="Id" label="ID" show-overflow-tooltip />
        <el-table-column prop="Username" label="用户名" show-overflow-tooltip />
        <el-table-column prop="LastTimeString" label="最后登录时间" show-overflow-tooltip />
        <el-table-column prop="Status" label="状态" show-overflow-tooltip>
          <template #default="scope">
            <el-tag :type="scope.row.Status === 1 ? 'info' : 'success'">
              {{ scope.row.StatusString }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="CreatedAtString" label="创建时间" show-overflow-tooltip />
        <el-table-column label="操作" fixed="right">
          <template #default="scope">
            <el-button type="text" size="small" @click="handleEdit(scope.row)">更新</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <el-dialog
      title="更新管理员"
      v-model="dialogVisible"
      width="500px"
      :before-close="handleClose"
    >
      <el-form
        ref="userFormRef"
        :model="userForm"
        :rules="userRules"
        label-width="100px"
      >
        <el-form-item label="用户名" prop="Username">
          <el-input v-model="userForm.Username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="userForm.Password" placeholder="如需修改请输入新密码" show-password />
        </el-form-item>
        <el-form-item label="状态" prop="Status">
          <el-radio-group v-model="userForm.Status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
 </template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { manageUserList, updateManageUser } from '@/api/manageUser'

const loading = ref(false)
const list = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const dialogVisible = ref(false)
const userFormRef = ref()

const userForm = reactive({
  Id: 0,
  Username: '',
  Password: '',
  Status: 0,
})

const userRules = {
  Username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  Password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  Status: [{ required: true, message: '请选择状态', trigger: 'change' }],
}

const getList = async () => {
  loading.value = true
  try {
    const res = await manageUserList({
      Page: currentPage.value,
      PageSize: pageSize.value,
    })
    if (res.code === 200) {
      list.value = res.data?.List || []
      total.value = res.data?.Total || 0
    } else {
      ElMessage.error(res.message || '获取管理员列表失败')
    }
  } catch (error) {
    ElMessage.error('获取管理员列表失败')
  } finally {
    loading.value = false
  }
}

const handleSizeChange = (size) => {
  pageSize.value = size
  getList()
}

const handleCurrentChange = (page) => {
  currentPage.value = page
  getList()
}

const handleEdit = (row) => {
  dialogVisible.value = true
  Object.assign(userForm, {
    Id: row.Id,
    Username: row.Username || '',
    Password: '',
    Status: row.Status,
  })
}

const handleClose = () => {
  dialogVisible.value = false
}

const handleSubmit = async () => {
  if (!userFormRef.value) return
  try {
    await userFormRef.value.validate()
    const payload = {
      Id: Number(userForm.Id),
      Username: userForm.Username,
      Status: Number(userForm.Status) || null,
    }
    if (userForm.Password && userForm.Password.trim()) {
      payload.Password = userForm.Password
    }
    const res = await updateManageUser(payload)
    if (res.code === 200) {
      ElMessage.success('更新成功')
      dialogVisible.value = false
      getList()
    } else {
      ElMessage.error(res.message || '更新失败')
    }
  } catch (error) {
    // ignore
  }
}

onMounted(() => {
  getList()
})
</script>

<style scoped>
.user-list-container {
  padding: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

::deep(.el-table .cell) {
  white-space: nowrap;
}
:deep(.el-scrollbar__thumb) {
  background-color: #909399;
  opacity: 1;
}
</style>